package cn.ztf.zshop.sys.service.impl;

import cn.ztf.zshop.common.exception.ZShopException;
import cn.ztf.zshop.sys.dao.SysDeptDao;
import cn.ztf.zshop.sys.dao.SysUserDao;
import cn.ztf.zshop.sys.dto.SysDeptDto;
import cn.ztf.zshop.sys.po.SysDept;
import cn.ztf.zshop.sys.po.SysUser;
import cn.ztf.zshop.sys.service.SysDeptService;
import cn.ztf.zshop.sys.service.SysUserService;
import cn.ztf.zshop.sys.vo.SysDeptVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class SysDeptServiceImpl extends ServiceImpl<SysDeptDao, SysDept> implements SysDeptService {

    private final MapperFacade mapperFacade;

    private final SysUserDao sysUserDao;

    @Override
    public List<SysDeptVo> listTree() {
        List<SysDept> deptList = lambdaQuery().orderByAsc(SysDept::getParentId).list();
        LinkedList<SysDeptVo> list = new LinkedList<>();
        HashMap<Long, SysDeptVo> map = new HashMap<>();
        for (SysDept sysDept : deptList) {
            SysDeptVo deptVo = mapperFacade.map(sysDept, SysDeptVo.class);
            map.put(deptVo.getDeptId(), deptVo);
            if (sysDept.getParentId()==0) {
                list.add(deptVo);
                continue;
            }
            SysDeptVo parent = map.get(sysDept.getParentId());
            if (ObjectUtils.isEmpty(parent)) {
                continue;
            }
            List<SysDeptVo> children = parent.getChildren();
            if (ObjectUtils.isEmpty(children)) {
                children = new LinkedList<>();
                parent.setChildren(children);
            }
            children.add(deptVo);
        }
        return list;
    }

    @Override
    public SysDeptVo getSysDeptById(Long id) {
        SysDept sysDept = baseMapper.selectById(id);
        SysDeptVo sysDeptVo = mapperFacade.map(sysDept, SysDeptVo.class);
        return sysDeptVo;
    }

    @Override
    public void create(SysDeptDto sysDeptDto) {
        SysDept sysDept = mapperFacade.map(sysDeptDto, SysDept.class);
        baseMapper.insert(sysDept);
    }

    @Override
    public void updateDept(SysDeptDto sysDeptDto) {
        SysDept sysDept = mapperFacade.map(sysDeptDto, SysDept.class);
        baseMapper.updateById(sysDept);
    }

    @Override
    public void delete(Long deptId) {
        // 查看是否有人正在此部门
        Long userCount = new LambdaQueryChainWrapper<>(sysUserDao)
                .eq(SysUser::getDeptId, deptId)
                .count();
        if (userCount > 0) {
            throw new ZShopException("当前部门含有员工,无法删除");
        }
        baseMapper.deleteById(deptId);
    }

    /**
     * 根据部门id查询用户列表
     * @param deptId
     * @return
     */
    @Override
    public List<SysDeptVo> userList(Long deptId) {
        List<SysUser> users = new LambdaQueryChainWrapper<SysUser>(sysUserDao).eq(SysUser::getDeptId, deptId).list();
        List<SysDeptVo> list = new LinkedList<>();
        for (SysUser user : users) {
            SysDeptVo sysDeptVo = mapperFacade.map(user, SysDeptVo.class);
            list.add(sysDeptVo);
        }
        return list;
    }
}
